package com.android.kotlincoroutinemvvmdemo.view

import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R

class CurveBezierView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = context.resources.getColor(R.color.color_da8989)
            //color = context.resources.getColor(R.color.color_00000000)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_B128C9)
        }
    }

    private val mRadius = context.resources.getDimension(R.dimen.dp_5)

    private var cx = 0F
        set(value) {
            field = value
            invalidate()
        }

    private var cy = 0F
        set(value) {
            field = value
            invalidate()
        }

    private val mPath by lazy {
        Path()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)

        cx = 20F
        cy = mHeight.toFloat() - 20F
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))

        canvas?.apply {
            mPath.moveTo(20F, mHeight.toFloat() - 20F)
            mPath.cubicTo(
                mWidth / 3.0F - 15F,
                mHeight / 3.0F + 30F,
                mWidth * 2 / 3.0F + 45F,
                mHeight * 2 / 3.0F - 30F,
                mWidth.toFloat() - 20F,
                20F
            )
            drawPath(mPath, mPaint0)

            //绘制运动小圆
            drawCircle(cx, cy, mRadius, mPaint1)
        }
    }

    fun startAnimation() {
        val objectAnimator = ObjectAnimator.ofFloat(this, "cx", "cy", mPath)
        objectAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}
            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {}
            override fun onAnimationEnd(animation: Animator) {
                //Log.d(TAG,"---onAnimationEnd---")
                //mStartAngle += floatArray[i]
            }
        })

        objectAnimator.duration = 5000
        //使用差值器避免中间会出现的停顿
        objectAnimator.interpolator = LinearInterpolator()
        objectAnimator.repeatMode = ValueAnimator.REVERSE
        objectAnimator.repeatCount = ValueAnimator.INFINITE
        objectAnimator.start()
    }
}